<?php

/*
* ServiceGroup Controller class
* PHP versions 5.1.4
* @filesource
* @author   Manisha/Reetu
* @version 0.0.1 
*   - Initial release 
*/ 
class ServiceGroupsController extends AppController {
    /**
    * @Method      :beforeFilter
    * @Description : Parent Before filter calling
    * @access      :public
    * @param      :null
    * @return     :null
    */
    public function beforeFilter(){
        $this->layout = 'home';
        parent::beforeFilter();
    }
    
    /**
    * @Method       :index
    * @Description  :to set up the Lisitng of Service Units
    * @access       :public
    * @param        :null
    * @return       :null
    */
    public function index() {
        $this->layout = 'home';
	$userId   = $this->Auth->User("id");
	$userType = $this->Auth->User('user_type');
	
	$savecrit     = '';
	    
	//criteria array for search
	$crit 	= array();
	$crit[] = "";
	
	if(isset($this->data)){
	 
	    if(isset($this->data['ServiceGroup']['search_criteria']) &&  $this->data['ServiceGroup']['search_criteria'] != ''){
		   $searchCriteriaTerm=trim($this->data['ServiceGroup']['search_criteria']);
		//will search with name and abbreviation
		$crit[]    = "(ServiceGroup.group_name like '%".$searchCriteriaTerm."%')";			
		$savecrit .= "search_criteria:".$searchCriteriaTerm."/";
	    }
	}
	//for passing the values of search conditions in params of paging to next page
	if(count($this->params['named']) > 0){
	    foreach($this->params['named'] as $paramKey => $paramValue){
		$savecrit .= $paramKey.":".$paramValue."/";
	    }
	    if(isset($this->params['named']['search_criteria']))
		$crit[] = "(ServiceGroup.group_name like '%".$this->params['named']['search_criteria']."%')";
	}//if
	if(count($crit) > 0){
	    $criteria = implode("1 AND ", $crit) ;
	}else{
	    $criteria = "";	
	}

	App::Import("Model", "ServiceGroup");
	$this->ServiceGroup = new ServiceGroup();
	
	$unitGrpMainData = $this->paginate('ServiceGroup', array($criteria));
			    	 
	$this->set('unitGrpMainData', $unitGrpMainData);

	if($this->request->is('ajax')) {
	    $this->viewPath = 'elements'.DS.'servicegroups';
	    $this->render('unitgrpmaintenance', 'ajax');
	}//if
    }
    
    /**
    * @Method       :add
    * @Description  :to add Service Units
    * @access       :public
    * @param        :null
    * @return       :null
    */
    public function add() {
        $userId = $this->Auth->User("id");
        $userType = $this->Auth->User('user_type');

        if ($this->request->is('post')) {
            $this->ServiceGroup->create();
            if ($this->ServiceGroup->save($this->request->data)) {
		$msg = __("SERVICE_GROUP_ADD_SUCCESS", true);
		$this->Session->setFlash('<div class="totalSuccess">' . $msg . '</div>');
                return $this->redirect(array('action' => 'index'));
            }
	    $msg = __("SERVICE_GROUP_ADD_FAILURE", true);
            $this->Session->setFlash('<div class="totalErrors">' . $msg . '</div>');
        }
    }
    
    /**
    * @Method       :edit
    * @Description  :to edit Service Units
    * @access       :public
    * @param        :null
    * @return       :null
    */
    public function edit($id = null) {
        $userId   = $this->Auth->User("id");
	$userType = $this->Auth->User('user_type');
        
        
        if (!$id) {
            throw new NotFoundException(__('INVALID_SERVICE_GROUP'));
        }
        $serviceGroup = $this->ServiceGroup->findById($id);
        
        if (!$serviceGroup) {
            throw new NotFoundException(__('INVALID_SERVICE_GROUP'));
        }
        if ($this->request->is(array('ServiceGroup', 'put'))) {
            $this->ServiceGroup->id = $id;
            if ($this->ServiceGroup->save($this->request->data)) {
		$msg = __("SERVICE_GROUP_UPDATE_SUCCESS", true);
		$this->Session->setFlash('<div class="totalSuccess">' . $msg . '</div>');
                return $this->redirect(array('action' => 'index'));
            }
	    $msg = __("SERVICE_GROUP_UPDATE_FAILURE", true);
	    $this->Session->setFlash('<div class="totalErrors">' . $msg . '</div>');
        }
        if (!$this->request->data) {
            $this->request->data = $serviceGroup;
        }
    }    
    
    public function view($serviceGroupID = null) {
        $userId = $this->Auth->User("id");
        $userType = $this->Auth->User('user_type');
        
        App::Import("Model", "ServiceUnit");
	$this->ServiceUnit = new ServiceUnit();
	$this->ServiceUnit->recursive = -1;
        
	$serviceUnitData = $this->paginate('ServiceUnit', array('ServiceUnit.service_group_id' => $serviceGroupID));
		//pr($serviceUnitData);	    	 
	$this->set('serviceUnitData', $serviceUnitData);
        if($this->request->is('ajax')) {
	    $this->viewPath = 'elements'.DS.'servicegroups';
	    $this->render('unit_list', 'ajax');
	}//if
    }
}